package com.itheima.user.service.impl;

import com.itheima.article.feign.ApAuthorFeign;
import com.itheima.article.pojo.ApAuthor;
import com.itheima.common.pojo.Result;
import com.itheima.user.mapper.ApUserMapper;
import com.itheima.user.pojo.ApUser;
import com.itheima.user.pojo.ApUserRealname;
import com.itheima.user.mapper.ApUserRealnameMapper;
import com.itheima.user.service.ApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.wemedia.feign.WmUserFeign;
import com.itheima.wemedia.pojo.WmUser;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author czh
 * @since 2021-08-20
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {


    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private WmUserFeign wmUserFeign;
    @Autowired
    private ApAuthorFeign apAuthorFeign;
    /**
     * 驳回用户认证
     * @param id
     * @param reason
     * @return
     */
    @Override
    public boolean reject(Integer id, String reason) {
        //构造修改对象
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(id);
        apUserRealname.setReason(reason);
        apUserRealname.setStatus(2);
        apUserRealname.setUpdatedTime(LocalDateTime.now());

        apUserRealnameMapper.updateById(apUserRealname);
        return false;
    }

    /**
     * 通过用户认证
     * @param id
     * @return
     */
    @Override
    public boolean pass(Integer id) {
        //修改实名认证表为通过状态，status = 9
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(id);
        apUserRealname.setStatus(9);
        apUserRealname.setUpdatedTime(LocalDateTime.now());

        apUserRealnameMapper.updateById(apUserRealname);
        //查询修改的这个用户的id
        ApUserRealname userRealname = apUserRealnameMapper.selectById(id);
        if (userRealname != null) {
            //得到用户id
            Integer userId = userRealname.getUserId();
            //根据用户id修改user表中的身份认证状态为true
            ApUser apUser = new ApUser();
            apUser.setId(userId);
            apUser.setIsIdentityAuthentication(1);
            apUserMapper.updateById(apUser);
            //得到用户信息
            ApUser newApUser = apUserMapper.selectById(userId);

            //定义自媒体账号id
            int wmUserId = 0;
            //自媒体账号同步
            //先判断该自媒体账号是否存在
            Result<WmUser> wmUserResult = wmUserFeign.getByUserId(userRealname.getUserId());
            if (wmUserResult.getData() == null){
                //不存在则添加，存在不管
                WmUser wmUser = new WmUser();
                BeanUtils.copyProperties(newApUser,wmUser);
                wmUser.setType(0);
                wmUser.setApUserId(newApUser.getId());
                wmUser.setCreatedTime(LocalDateTime.now());
                //添加
                Result<WmUser> result = wmUserFeign.insert(wmUser);
                wmUserId = result.getData().getId();
            }
            //作者账号同步
            Result<ApAuthor> apAuthorResult = apAuthorFeign.getByUserId(userRealname.getUserId());
            if (apAuthorResult.getData() == null){
                ApAuthor apAuthor = new ApAuthor();
                BeanUtils.copyProperties(newApUser,apAuthor);
                apAuthor.setType(0);
                apAuthor.setUserId(newApUser.getId());
                apAuthor.setCreatedTime(LocalDateTime.now());
                if (wmUserId != 0) {
                    apAuthor.setWmUserId(wmUserId);
                }
                //添加
                apAuthorFeign.insert(apAuthor);
                return true;
            }


        }



        return false;
    }


}
